package com.vote.util;

import java.util.regex.Pattern;

public class RegexUtil {
	/**
	 * 判断输入Email地址是否合法
	 * 
	 * @param email
	 * @return boolean
	 */
	public static boolean isEmail(String email) {
		String emailRegex = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";// Emial的正则表达式
		boolean isEmail = Pattern.matches(emailRegex, email);
		return isEmail;
	}

	/**
	 * 判断输入电话号码是否合法
	 * 
	 * @param telephone
	 * @return boolean
	 */
	public static boolean isTelephone(String telephone) {
		String telRegex = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$|(013?\\d{9}$)|(13?\\d{9}$)|(15?\\d{9}$)|(18?\\d{9}$)";
		boolean isTelephone = Pattern.matches(telRegex, telephone);
		return isTelephone;
	}

	/**
	 * 判断输入字符串是否由26英文字母组成
	 * 
	 * @param letter
	 * @return
	 */
	public static boolean isLetter(String letter) {
		String letterReg = "^[A-Za-z]+$";
		boolean isLetter = Pattern.matches(letterReg, letter);
		return isLetter;
	}

	/**
	 * 判断输入字符串是否由字母和数字组成
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isLetterOrNumber(String string) {
		String stringReg = "^[A-Za-z0-9]+$";
		boolean isLetterOrNumber = Pattern.matches(stringReg, string);
		return isLetterOrNumber;
	}

	/**
	 * 判断输入日期格式是否为xxxx-yy-mm
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isDate(String string) {
		String dateReg = "^\\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\\d|3[0-1])$";
		boolean isDate = Pattern.matches(dateReg, string);
		return isDate;
	}

	/**
	 * 判断输入日期格式是否为yyyy-MM-dd hh:mm
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isDateTime(String string) {
		String dateReg = "\\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]\\s|[1-2]\\d\\s|3[0-1]\\s)(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5]\\d):(0?[0-9]|[1-5]\\d)$";
		return Pattern.matches(dateReg, string);
	}

	/**
	 * 判读输入数字是否为整数 
	 * @param integer
	 * 		需要验证的数字
	 * @return
	 */
	public static boolean isInteger(int integer) {
		String integerReg = "^-?[1-9]\\d*$";
		return Pattern.matches(integerReg, String.valueOf(integer));
	}

}
